﻿package  
{
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	
	public class AnimatingSprite extends Bitmap
	{
		
		// Properties /////////////////////////////////////////////////////////////////
		
		/**
		 * The buffer to store the whole bitmap.
		 * @private
		 */
		private var _spriteBuffer:BitmapData;
			
		/**
		 * The number of frames that the animated bitmap has.
		 * @private
		 */
		private var _frameAmount:int;
		
		/**
		 * Used for the onTimer function to count loops.
		 * @private
		 */
		private var _frameNr:int;
		
		/**
		 * Determines if the animTile is already playing or not.
		 * @private
		 */
		
		 //come back and don't make this public
		public var _isPlaying:Boolean;
		
		/**
		 * Point object used for copyPixels operation.
		 * @private
		 */
		private var _point:Point;
		
		/**
		 * Rectangle object used for copyPixels operation.
		 * @private
		 */
		private var _rect:Rectangle;
		
				/**
		 * Timer used for frame animation.
		 * @private
		 */
		private var _timer:FrameTimer;
		
		
		private var _yOffset:uint = 0;
		private var _xOffset:uint = 0;
		
		private var _currentWidth:int = 0;
		
		public var _currentAnimation:String = "idle";
		

		// Constructor ////////////////////////////////////////////////////////////////
		
		
/**
		 * Creates a new AnimatingSprite instance.
		 * 
		 * @param bitmap The bitmapData object that contains the image sequence
		 *         for the animated bitmap.
		 * @param w The width of the animated bitmap.
		 * @param h The height of the animated bitmap.
		 * @param timer The frame rate timer used for the animated bitmap.
		 */
		public function AnimatingSprite(bitmap:BitmapData,
										   w:int,
										   h:int,
										   timer:FrameTimer,
										   transparent:Boolean = true,
										   pixelSnapping:String = "auto",
										   smoothing:Boolean = false)
		{
			super(new BitmapData(w, h, transparent, 0x00000000), pixelSnapping, smoothing);
			_spriteBuffer = bitmap.clone();
			
			//_frameAmount = _spriteBuffer.width / width;
			//get frame amount in correct way
			
			_frameAmount = 16;
			_frameNr = 1;
			_isPlaying = false;
			_timer = timer;
			_point = new Point(0, 0);
			_rect = new Rectangle(0, 0, w, h);
			bitmapData.copyPixels(_spriteBuffer, _rect, _point);
			_currentWidth = w;
		}
		
				/**
		 * Draws the next bitmap frame from the buffer to the animated bitmap.
		 * @private
		 */
		private function draw():void
		{
			//dispatchEvent(new FrameEvent(FrameEvent.ENTER));
			//_yOffset = 15;
			_rect = new Rectangle((_frameNr - 1) * width +_xOffset, _yOffset, width, height);

			
			bitmapData.copyPixels(_spriteBuffer, _rect, _point);
			
			
			
			
			
		}
		
		public function changeAnimation(key:String):void
		{
			if (key == "idle")
			{
				this._frameAmount = 1;
				this._frameNr = 1;
				this._yOffset = 160;
				this._xOffset = 101.5 * 15;
				this._currentAnimation = "idle";
				this.stop();
				this.play();
			}
			if (key == "run")
			{
				this._frameAmount = 16;
				this._frameNr = 1;
				this._yOffset = 0;
				this._xOffset = 0;
				this._currentAnimation = "run";
				this.stop();
				this.play();
			}
			
			if (key == "jump")
			{
				this._frameAmount = 1;
				this._frameNr = 1;
				this._yOffset = 160;
				this._xOffset = 0;
				this._currentAnimation = "jump";
				this.stop();
				this.play();
			}
			if (key == "swing")
			{
				this._frameAmount = 1;
				this._frameNr = 1;
				this._yOffset = 600;
				this._xOffset = 0;
				this._currentAnimation = "swing";
				this.stop();
				this.play();
			}
			if (key == "throw")
			{
				this._frameAmount = 5;
				this._frameNr = 1;
				this._yOffset = 600;
				this._xOffset = 125 * 9;
				this._currentAnimation = "throw";
				this.stop();
				this.play();
			}
		}
		
		public function changeOffset(offset:uint)
		{
			
			_yOffset = offset;
			
		}
		
				/**
		 * Stops the playback of the animated bitmap.
		 */
		public function stop():void
		{
			if (_isPlaying)
			{
				//this is to stop the global timer turning off possible jerky-ness cuaser
				//_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, playForward);
				_isPlaying = false;
			}
		}
		
		public function play():void
		{
			if (!_isPlaying)
			{
				_isPlaying = true;
				_timer.addEventListener(TimerEvent.TIMER, playForward);
				_timer.start();
			}
			else
			{
				stop();
				playForward();
				play();
			}
		}
				/**
		 * Plays the animation forward by one frame.
		 * @private
		 */
		private function playForward(event:TimerEvent = null):void
		{
			_frameNr++;
			if (_frameNr > _frameAmount)
			{
				if (_currentAnimation == "throw")
				{
					this.changeAnimation("swing");
				}
				_frameNr = 1;
			}
			else if (_frameNr < 1) _frameNr = _frameAmount;
			draw();
		}
		
	}
	
}